// src/Main.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    private static ArrayList<Income> incomes = new ArrayList<>();
    private static ArrayList<Expense> expenses = new ArrayList<>();
    private static BudgetManager budgetManager = new BudgetManager();
    private static MonthlyStatistics monthlyStatistics = new MonthlyStatistics();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            showMenu();
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    addIncome();
                    break;
                case "2":
                    addExpense();
                    break;
                case "3":
                    displayAllRecords();
                    break;
                case "4":
                    queryRecords();
                    break;
                case "5":
                    setMonthlyBudget();
                    break;
                case "6":
                    viewMonthlyStatistics();
                    break;
                case "7":
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("无效的选项，请重新选择");
                    break;
            }
        }
    }

    private static void showMenu() {
        System.out.println("\n请选择操作: ");
        System.out.println("1. 记录收入");
        System.out.println("2. 记录支出");
        System.out.println("3. 展示所有账单");
        System.out.println("4. 查询账单");
        System.out.println("5. 设置月度预算");
        System.out.println("6. 查看月度统计");
        System.out.println("7. 退出");
    }

    private static void addIncome() {
        System.out.print("输入日期 (YYYY-MM-DD): ");
        String date = scanner.nextLine();
        System.out.print("输入收入金额: ");
        double amount = Double.parseDouble(scanner.nextLine());
        System.out.print("输入收入类别 (如工资、奖金等): ");
        String category = scanner.nextLine();
        System.out.print("输入备注: ");
        String remark = scanner.nextLine();

        try {
            Income income = new Income(date, amount, category, remark);
            incomes.add(income);
            System.out.println("收入记录添加成功！");
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }

    private static void addExpense() {
        System.out.print("输入日期 (YYYY-MM-DD): ");
        String date = scanner.nextLine();
        System.out.print("输入支出金额: ");
        double amount = Double.parseDouble(scanner.nextLine());
        System.out.print("输入支出类别 (如餐饮、交通等): ");
        String category = scanner.nextLine();
        System.out.print("输入备注: ");
        String remark = scanner.nextLine();

        try {
            Expense expense = new Expense(date, amount, category, remark);
            expenses.add(expense);
            System.out.println("支出记录添加成功！");
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }

    private static void displayAllRecords() {
        System.out.println("\n--- 所有收入记录 ---");
        incomes.forEach(System.out::println);
        System.out.println("\n--- 所有支出记录 ---");
        expenses.forEach(System.out::println);
    }

    private static void queryRecords() {
        System.out.print("查询类型 (1-日期, 2-日期范围, 3-类别): ");
        String type = scanner.nextLine();

        switch (type) {
            case "1":
                System.out.print("输入查询日期 (YYYY-MM-DD): ");
                String date = scanner.nextLine();
                incomes.stream().filter(i -> i.getDate().equals(date)).forEach(System.out::println);
                expenses.stream().filter(e -> e.getDate().equals(date)).forEach(System.out::println);
                break;

            case "2":
                System.out.print("输入起始日期 (YYYY-MM-DD): ");
                String startDate = scanner.nextLine();
                System.out.print("输入结束日期 (YYYY-MM-DD): ");
                String endDate = scanner.nextLine();
                incomes.stream().filter(i -> i.getDate().compareTo(startDate) >= 0 && i.getDate().compareTo(endDate) <= 0).forEach(System.out::println);
                expenses.stream().filter(e -> e.getDate().compareTo(startDate) >= 0 && e.getDate().compareTo(endDate) <= 0).forEach(System.out::println);
                break;

            case "3":
                System.out.print("输入查询类别: ");
                String category = scanner.nextLine();
                incomes.stream().filter(i -> i.getCategory().equals(category)).forEach(System.out::println);
                expenses.stream().filter(e -> e.getCategory().equals(category)).forEach(System.out::println);
                break;

            default:
                System.out.println("无效的查询类型");
                break;
        }
    }

    private static void setMonthlyBudget() {
        System.out.print("设置月度预算: ");
        double budget = Double.parseDouble(scanner.nextLine());
        budgetManager.setMonthlyBudget(budget);
        System.out.println("月度预算设置为: " + budget + " 元");
    }

    private static void viewMonthlyStatistics() {
        System.out.println("\n--- 月度统计 ---");
        monthlyStatistics.displayStatistics(incomes, expenses);

        double totalExpense = expenses.stream().mapToDouble(Expense::getAmount).sum();
        double remainingBudget = budgetManager.calculateRemainingBudget(totalExpense);
        System.out.printf("剩余可用预算: %.2f 元\n", remainingBudget);
    }
}
